package com.paperobot.common.util;

import java.util.ArrayList;
import java.util.List;

import com.paperobot.model.News;
import com.paperobot.model.Paper;
import com.paperobot.model.PaperType;
import com.paperobot.model.vo.PaperVO;

public class VOHelper {

	
	public static String getTypeName(int type, List<PaperType> paperTypeList){
		String typeName = null;
		for (PaperType paperType : paperTypeList) {
			if(type == paperType.getType()){
				typeName = paperType.getTypeName();
			}
		}
		return typeName;
	}
	
	public static String getSubTypeName(int type, int subType, List<PaperType> paperTypeList){
		
		String subTypeName = null;
		for (PaperType paperType : paperTypeList) {
			if(type == paperType.getType() && subType == paperType.getSubType()){
				subTypeName = paperType.getSubTypeName();
			}
		}
		return subTypeName;
	}

	public static PaperVO paperToVO(Paper paper, List<PaperType> paperTypeList ){
	
		if(null!=paper && null != paperTypeList){
			PaperVO paperVO = new PaperVO();
			paperVO.setPaperId(paper.getPaperId());
			paperVO.setType(paper.getType());
			paperVO.setTypeName(getTypeName(paper.getType(), paperTypeList));
			paperVO.setSubType(paper.getSubType());
			paperVO.setSubTypeName(getSubTypeName(paper.getType(), paper.getSubType(), paperTypeList));
			paperVO.setTitle(paper.getTitle());
			paperVO.setAbstracts(paper.getAbstracts());
			paperVO.setKeywords(paper.getKeywords());
			paperVO.setContent(paper.getContent());
			paperVO.setAuthor(paper.getAuthor());
			paperVO.setSource(paper.getAuthor());
			paperVO.setCreateTime(paper.getCreateTime());
			paperVO.setUpdateTime(paper.getUpdateTime());
			paperVO.setStatus(paper.getStatus());
			paperVO.setClickNum(paper.getClickNum());
			
			return paperVO;
		}else{
			return null;
		}

	}
	
	public static List<PaperVO> paperToVO(List<Paper> paperList, List<PaperType> paperTypeList){
		
		List<PaperVO> paperVOList = new ArrayList<PaperVO>();
		for (Paper paper : paperList) {
			paperVOList.add(paperToVO(paper, paperTypeList));
		}
		return paperVOList;
	}
	
	public static PaperVO convertPaperTitle(PaperVO paperVO){
		if(null!=paperVO){
			if(paperVO.getTitle().length() > 17){
				String title = paperVO.getTitle().substring(0, 17) + "...";
				paperVO.setTitle(title);
			}
			return paperVO;
		}else{
			return null;
		}
	}
	public static List<PaperVO> convertPaperTitle(List<PaperVO> paperVOList){
		for (PaperVO paperVO : paperVOList) {
			convertPaperTitle(paperVO);
		}
		return paperVOList;
	}
	
	public static Paper VOToPaper(PaperVO paperVO ){
		
		if(null != paperVO ){
			Paper paper = new Paper();
			paper.setPaperId(paperVO.getPaperId());
			paper.setType(paperVO.getType());
			paper.setSubType(paperVO.getSubType());
			paper.setTitle(paperVO.getTitle());
			paper.setAbstracts(paperVO.getAbstracts());
			paper.setKeywords(paperVO.getKeywords());
			paper.setContent(paperVO.getContent());
			paper.setAuthor(paperVO.getAuthor());
			paper.setSource(paperVO.getAuthor());
			paper.setCreateTime(paperVO.getCreateTime());
			paper.setUpdateTime(paperVO.getUpdateTime());
			paper.setStatus(paperVO.getStatus());
			paper.setClickNum(paperVO.getClickNum());
			
			return paper;
		}else{
			return null;
		}
	}
	
	public static List<Paper> VOToPaper(List<PaperVO> paperVOList){
		
		List<Paper> paperList = new ArrayList<Paper>();
		for (PaperVO paperVO : paperVOList) {
			paperList.add(VOToPaper(paperVO));
		}
		return paperList;
	}
	
	public static News convertPaperTitle(News news){
		if(null!=news){
			if(news.getTitle().length() > 17){
				String title = news.getTitle().substring(0, 17) + "...";
				news.setTitle(title);
			}
			return news;
		}else{
			return null;
		}
	}
	
	public static List<News> convertNewsTitle(List<News> newsList){
		for (News news : newsList) {
			convertPaperTitle(news);
		}
		return newsList;
	}
}












